<template>
  <card-page style="width: 600px; margin-left: auto; margin-right: auto;">
    <el-form
      ref="form"
      :model="form" 
      label-width="140px"
      style="width: 500px;margin: 0 auto"
    >
      <el-form-item
        prop="description"
        label="服务内容"
        :rules="[{required: true, message: ' '}]"
      >
        <el-input type="textarea" :rows="4" v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item
        prop="taskType"
        label="类型"
        :rules="[{required: true, message: ' '}]">
        <el-radio-group v-model="form.taskType">
          <el-radio :label="0">单人</el-radio>
          <el-radio :label="1">多人</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        prop="profession"
        label="服务领域"
        :rules="[{required: true, message: ' '}]"
      >
        <type-select
          v-model="form.profession"
          :items="professions"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item
        prop="taskIntegral"
        label="服务奖励积分"
        :rules="[{required: true, message: ' '}]"
      >
        <el-input-number v-model="form.taskIntegral" :precision="0" :min="0" :controls="right" />
      </el-form-item>
      <el-form-item
        prop="linkName"
        label="服务对象姓名"
        :rules="[{required: true, message: ' '}]"
      >
        <el-input v-model.trim="form.linkName"></el-input>
      </el-form-item>
      <el-form-item
        prop="linkPhone"
        label="服务对象电话"
        :rules="[{required: true, message: ' '}]">
        <el-input v-model.trim="form.linkPhone"></el-input>
      </el-form-item>
      <el-form-item
        prop="orgId"
        label="所属社区"
        :rules="[{required: true, message: ' '}]">
        <org-select v-model="form.orgId" />
      </el-form-item>
      <el-form-item
        v-if="mode == 'add'"
        prop="area"
        label="服务地址-地区"
      >
        <area-select v-model="area" style="width: 100%;"></area-select>
      </el-form-item>
      <el-form-item
        prop="address"
        label="服务地址-详细"
        :rules="[{required: true, message: ' '}]"
      >
        <el-input type="textarea" :rows="1" v-model.trim="form.address"></el-input>
      </el-form-item>
      <el-form-item
        prop="planDateAndServiceTime"
        label="服务时间"
        :rules="[{required: true, message: ' '}]"
      >
        <el-date-picker
          type="datetime"
          placeholder="选择时间"
          v-model="form.planDateAndServiceTime" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" :loading="submitting">确定</el-button>
      </el-form-item>
    </el-form>
  </card-page>
</template>

<script>
import { Select } from 'element-ui';
import moment from 'moment';

export default {
  pageProps: {
    title: ({params}) => `${params.mode == 'add' ? '发布' : '修改'}志愿者任务`
  },
  components: {
    OrgSelect: () => import('@/pages/org/OrgSelect'),
    AreaSelect: () => import('@/components/area-select/AreaSelect')
  },
  data() {
    return {
      mode: this.$params.mode,
      form: {
        linkName: '',
        linkPhone: '',
        profession: [],
        taskIntegral: 0,
        serviceDuration: 1,
        orgId: null,
        address: '',
        description: ''
      },
      professions: [],
      area: {prov: '湖北省', city: '武汉市'},
      submitting: false
    }
  },
  async mounted() {
    this.professions = (await this.$http.get('/api/volunteer/profession'))
      .map(item => ({value: item.key, text: item.value}));
    if (this.mode == 'update') {
      let help = await this.$http.get('/api/volunteer/help/details', {params: {orderCode: this.$params.order.orderno}});
      this.form = help;
    }
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        this.submitting = true;
        let formData = {...this.form};
        if (this.mode == 'add') {
          formData.address = this.area.prov + this.area.city + this.area.dist + formData.address;
        }
        formData.city = this.area.city;
        const timeStrs = moment(formData.planDateAndServiceTime).format('YYYY-MM-DD HH:SS').split(' ');
        formData.planDate = timeStrs[0];
        formData.serviceTime = timeStrs[1];
        delete formData.planDateAndServiceTime;

        const ret = await this.$http.post('/api/volunteer/help/save', formData);
        this.submitting = false;
        if (ret.success) {
          this.$message.success((this.mode == 'add' ? '增加' : '修改') + '志愿者任务成功');
          this.$refs.form.resetFields();
          this.$params.onSuccess();
        } else {
          this.$message.error(ret.message);
        }
      });
    }
  }
}
</script>